package cn.hls.web.controller.biz;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Goods;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.GoodsService;
import cn.hls.web.controller.base.BaseController;

/**
 * @fileName: GoodsController.java
 * @package: cn.hls.web.controller.biz
 * @Description: 企业基本信息COntroller
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController<Goods> {
	@Resource(name = "goodsService")
	private GoodsService goodsService;
	
	 
	/**
	 * 表单
	 * 
	 * @param id
	 * @param show
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	public ModelAndView info(Long id, boolean show )
			throws Exception {
		Goods entity = null;
		if (id != null) {
			entity = goodsService.find(id);
		}
		if (show) {
			return new ModelAndView("goods/show").addObject("entity", entity); // 查看页面
		} else {
		
			return new ModelAndView("goods/info")
					.addObject("entity", entity);// 新增、修改页面
		}

	}

	/**
	 * 添加操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(Goods entity) throws Exception {
		if(!goodsService.exists(entity)){
			boolean flag = goodsService.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		}else{
			msg.setInfo("存在相同的全称或工商注册号信息!");
			msg.setStatus(201);
		}
		return msg;
	}

	 
	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		goodsService.delete(tempIds);
		return msg;
	}

	/**
	 * 修改操作
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Msg update(Goods entity) throws Exception {
		try {
			boolean flag = goodsService.update(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setInfo(e.getMessage());

		}
		return msg;
	}

	/**
	 * 分页查询
	 * 
	 * @param entity 查询的货物对象
	 * @param page 货物分页对象
	 * @return PageInfo<Goods>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Goods> list(Goods entity, PageInfo<Goods> page)
			throws Exception {
		return goodsService.loadGoodsInfo(entity, page);
	}
	
	@RequestMapping("/loadGoods")
	@ResponseBody
	public List<Goods> loadGoods(Goods entity)throws Exception{
		return goodsService.loadGoodsList(entity);
	}
	
	 
	@RequestMapping("/view")
	public ModelAndView view() {
		return new ModelAndView("/goods/list");

	}
  
}
